import random

import pygame

from actor.mine_boom import Boom, Mine
from dialog.actor_dialog import ArmyOneDialog, ArmyTwoDialog
from dialog.final_dialog import FinalTask3StartDialog, FinalTaskFailDialog, FinalTask3EndDialog
from scene.task.stage_one import TaskStatus
from scene.task.stage_one.task3_transmit_message import TransmitMessageTaskStatus
from scene.task.stage_two.task2_fight_find import FightAndFind

"""
援救红军任务类
"""


class SaveArmy(FightAndFind):
    def __init__(self, xiao_tie, japanese, mines_group, prizes_group, army_one, army_two, font):
        """
        初始化
        :param xiao_tie:
        :param japanese:
        :param mines_group:
        :param prizes_group:
        :param army_one:
        :param army_two:
        :param font:
        """
        super().__init__(xiao_tie, japanese, mines_group, prizes_group, font)
        self.army_one = army_one
        self.army_two = army_two
        # 初始状态
        self.state = TransmitMessageTaskStatus.finding_jian_jun
        # 人物对话
        self.army_one_dialog = ArmyOneDialog(self.army_one, font)
        self.army_two_dialog = ArmyTwoDialog(self.army_two, font)

    def init_dialog(self):
        """
        对话初始化
        :return:
        """
        self.task_dialog = FinalTask3StartDialog(self.font)
        self.win_dialog = FinalTask3EndDialog(self.font)
        self.fail_dialog = FinalTaskFailDialog(self.font)

    def draw_task(self, current_surface, x, y):
        """
        任务角色道具绘制
        :param current_surface:
        :param x:
        :param y:
        :return:
        """
        self.army_one.draw(current_surface, x, y)
        for mine in self.mines:
            mine.draw(current_surface, x, y)
        for boom in self.booms:
            boom.draw(current_surface, x, y)
        if self.state == TransmitMessageTaskStatus.found_jian_jun:
            self.army_one_dialog.draw(current_surface, x, y)

        elif self.state == TransmitMessageTaskStatus.found_tie_tou:
            self.army_two_dialog.draw(current_surface, x, y)

        if self.state >= TransmitMessageTaskStatus.finding_tie_tou:
            self.army_two.draw(current_surface, x, y)
        self.xiao_tie.draw(current_surface, x, y)

    def do_collide_group(self):
        """
        实现精灵之间的碰撞检测
        :return:
        """
        is_collide_japanese = pygame.sprite.spritecollide(self.xiao_tie, self.japanese, False)
        is_collide_mines_group = pygame.sprite.spritecollide(self.xiao_tie, self.mines_group, True)
        is_collide_mines = pygame.sprite.spritecollide(self.xiao_tie, self.mines, True)
        is_collide_prizes = pygame.sprite.spritecollide(self.xiao_tie, self.prizes_group, True)
        collide_japanese= pygame.sprite.groupcollide(self.mines, self.japanese, True, True)
        for wall in collide_japanese:
            self.sound1.play()
            self.create_boom(wall.rect.x - wall.rect.width,
                             wall.rect.y - wall.rect.height)
            self.xiao_tie.score += 5
            self.count += 1
            if self.count == 4 and self.state == TransmitMessageTaskStatus.ok:
                self.xiao_tie.weapon = "抢"
                self.xiao_tie.task3_start = False
                self.task_status = TaskStatus.win_dialog

        if is_collide_mines_group or is_collide_mines or is_collide_prizes:
            self.sound.play()
            if is_collide_prizes:
                self.xiao_tie.hp += 50
                self.xiao_tie.score += 1
                self.xiao_tie.shoot_count += 10
            else:
                self.xiao_tie.boom_count += 1
        if is_collide_japanese:
            if self.xiao_tie.time_stamp == 0:
                self.xiao_tie.hp -= random.randint(10, 25)
                self.xiao_tie.time_stamp = 10
            else:
                self.xiao_tie.time_stamp -= 1
            if self.xiao_tie.hp <= 0:
                self.task_status = TaskStatus.fail_dialog

    def do_collide_actor(self, army):
        """
        继承父类
        :param army:
        :return:
        """
        temp_rect = self.xiao_tie.rect
        self.xiao_tie.rect = pygame.Rect(self.xiao_tie.pos_x + 5,
                                         self.xiao_tie.pos_y + 5, 20, 60)
        is_collide = pygame.sprite.collide_rect(self.xiao_tie, army)
        self.xiao_tie.rect = temp_rect
        return is_collide

    def do_self_task(self, key_down, key_list):
        """
        具体任务内容
        :param key_down:
        :param key_list:
        :return:
        """
        self.xiao_tie.task3_start = True
        self.do_collide_group()
        if key_down and key_list[pygame.K_SPACE]:
            if self.xiao_tie.boom_count > 0:
                self.create_mine(self.xiao_tie.pos_x, self.xiao_tie.pos_y)
                self.xiao_tie.boom_count -= 1
        self.xiao_tie.time_limit -= 1
        if self.xiao_tie.time_limit <= 0:
            self.state = TransmitMessageTaskStatus.fail
            self.task_status = TaskStatus.fail_dialog

        if self.state == TransmitMessageTaskStatus.finding_jian_jun:
            if self.do_collide_actor(self.army_one):
                self.state = TransmitMessageTaskStatus.found_jian_jun

        elif self.state == TransmitMessageTaskStatus.found_jian_jun:
            if key_down and key_list[pygame.K_j]:
                self.army_one_dialog.next_text()
            if self.army_one_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.finding_tie_tou

        elif self.state == TransmitMessageTaskStatus.finding_tie_tou:
            if self.do_collide_actor(self.army_two):
                self.state = TransmitMessageTaskStatus.found_tie_tou

        elif self.state == TransmitMessageTaskStatus.found_tie_tou:
            if key_down and key_list[pygame.K_j]:
                self.army_two_dialog.next_text()
            if self.army_two_dialog.is_finish():
                self.xiao_tie.set_count(self.xiao_tie.boom_count + 5)
                self.state = TransmitMessageTaskStatus.ok
